<?php
require_once('JSON.php');
require_once('friendfeed.php');


define('EXPORT_FILE', 'ff_cache.php');
define('NB_ITEMS', 10);
define('USER', 'pollux');

$f = new FriendFeed(USER);
$entries = $f->fetch_user_feed(USER,null,0,NB_ITEMS);
$lastPublish = $entries->entries[0]->published;

$d = dirname(__FILE__);
if(!is_file($d . '/' . EXPORT_FILE))
{
    $fhandler = fopen($d . '/' . EXPORT_FILE, 'w');
}
else
{
    $lastPublishFile = fileatime($d . '/' . EXPORT_FILE);
    if(!($lastPublish > $lastPublishFile))
    {
    	include_once($d . '/' . EXPORT_FILE);
        return;
    }
    $fhandler = fopen($d . '/' . EXPORT_FILE, 'w+');
}

$ff_list_html = '';
foreach($entries->entries as $entry)
{
    $servicename = $entry->service->id;
    $title = $entry->title;
    $link = $entry->link;
    
    $class = '';
    $content = $title;
    if($servicename === 'twitter')
    {
        $class = 'liTwitter';
    }
    elseif($servicename === 'delicious')
    {
        $class = 'liDelicious';
    }
    elseif($servicename === 'blog')
    {
        continue;
    }
    elseif($servicename === 'flickr')
    {
        $class = 'liFlickr';
        $content = count($entry->media) . ' nouvelles photos sur mon compte Flickr.<br/>&nbsp;';
        //var_dump($entry);
        for($i = 0; $i < 3 and $i <= count($entry->media); $i++)
        {
            $content .= '<img src="'.$entry->media[$i]->thumbnails[0]->url.'" class="left mright"/>&nbsp;';
        }
        $content .= '<br class="clear"/>';
        //var_dump($entry->media);
    }
    elseif($servicename === 'googlereader')
    {
        $class = 'liGreader';
    }
    elseif($servicename === 'lastfm')
    {
    	$class = 'liLastfm';
    }
    elseif($servicename === 'librarything')
    {
    	$class = 'liLb';
    }
    $ff_list_html .= '<li class="'.$class.'">'.$content.'</li>';
}

fwrite($fhandler, $ff_list_html);

include_once($d . '/' . EXPORT_FILE);
?>